#include <stdlib.h>
#include <uuid/uuid.h>

#define NIT_SHORT_NAMES
#include <nit/macros.h>
#include <nit/palloc.h>
#include <nit/list.h>

#include "des.h"
#include "cell.h"
#include "pur.h"
#include "frm.h"
#include "fld.h"
#include "prd.h"

Inb_prd *
inb_prd_newf(uuid_t id, Inb_cell *args, Inb_fld *out, Inb_func func)
{
	Inb_prd *prd;
	Inb_des *des = inb_palloc(prd);

	pcheck(prd, NULL);
	des->type = PRD;
	des->flags = INB_FREE | INB_PRDF;
	prd = inb_thing(des);

	if (id)
		uuid_copy(prd->id, id);
	else
		uuid_generate(prd->id);

	prd->args = args;
	prd->out = out;
	prd->d.func = func;
	return prd;
}

Inb_prd *
inb_prd_newb(uuid_t id, Inb_cell *args, Inb_fld *out, Inb_cell *bod)
{
	Inb_prd *prd;
	Inb_des *des = inb_palloc(prd);

	pcheck(prd, NULL);
	des->type = PRD;
	des->flags = INB_FREE;
	prd = inb_thing(des);

	if (id)
		uuid_copy(prd->id, id);
	else
		uuid_generate(prd->id);

	prd->args = args;
	prd->out = out;
	prd->d.bod = bod;
	return prd;
}

void *
inb_prd_apply(Inb_prd *prd, Inb_cell *args)
{
	if (inb_des(prd)->flags & INB_PRDF)
		return prd->d.func(args);

	/* work on this! */
	return NULL;
}
